﻿B4J=true
Group=Default Group\请求处理器
ModulesStructureVersion=1
Type=Class
Version=10.3
@EndOfDesignText@
'' 模块全局变量
Sub Class_Globals
	Dim mysql As MySqlHelper
	Dim redis As RedisJdbcHelper
End Sub

'' 初始化相关对象
Public Sub Initialize
	LogHelper.WirteLog("请求进入系统处理模块")
	mysql.Initialize
	redis.Initialize
End Sub

'' 处理方法
'' 参数：req 为前端发来的请求对象
'' 参数：resp 为当前模块需要返回给客户端的对象
Sub Handle(req As ServletRequest, resp As ServletResponse)
	Dim baseUrl As String=req.RequestURI.ToLowerCase
	Dim httpMethod As String=req.Method
	''---------------根据请求方式和后缀路径作出对应的操作
	If httpMethod="POST" And baseUrl.EndsWith("login") Then
		AccountLogin(req,resp)
	Else If httpMethod="POST" And baseUrl.EndsWith("register") Then
		AccountRegister(req,resp)
	Else If httpMethod="POST" And baseUrl.EndsWith("forgotpassword") Then
		AccountForgotPassword(req,resp)
	Else If httpMethod="GET" And baseUrl.EndsWith("getuserinfo") Then
		GetUserInfo(req,resp)
	Else
		'' resp.Write($"不支持的方法${httpMethod}"$)
		AjaxResult.Error(resp,$"不支持的方法${httpMethod}"$,Null)
	End If
End Sub

'' 用户登录
Private Sub AccountLogin(req As ServletRequest, resp As ServletResponse)
	'' 解析post请求的参数
	Dim StreamReader As TextReader
	StreamReader.Initialize(req.InputStream)
	Dim jsn As JSONParser
	jsn.Initialize(StreamReader.ReadAll)
	Dim parms As Map= jsn.NextObject
	'' 判断是否传入参数
	If parms.IsInitialized=False Then
		'' resp.Write("请传入正确的参数")
		AjaxResult.Error(resp,"请传入正确的参数！",Null)
		Return
	End If
	'' 为了演示 打印前端参数
	For Each Key As String In parms.Keys
		Dim Value As Object = parms.Get(Key)
		''------
		LogHelper.WirteWranLog(Key & "-" & Value)
	Next
	'' 取出前端传入的参数
	Dim userName As String=parms.Get("email")
	Dim password As String=parms.Get("password")
	'' 校验参数等 这里省略
	'' 比对数据库账号密码 这里为了演示简单 比对后生成 jet token
	Dim result As List= mysql.ExecuteQueryByList("select * from sys_user where user_email=?",Array As String(userName))
	If result.IsInitialized=False Then
		'' resp.Write("用户不存在")
		AjaxResult.Error(resp,"用户不存在！",Null)
		Return
	End If
	''----------
	Dim user As Map=result.Get(0)
	'' 取出数据库参数
	Dim dbUserId As String=user.Get("user_id")
	Dim dbUserName As String=user.Get("user_email")
	Dim dbPassword As String=user.Get("user_password")
	'' 比对密码生成jwt,这里都是明文密码 演示用，实际需要加密
	If password <> dbPassword Then
		'' resp.Write("密码不正确")
		AjaxResult.Error(resp,"密码不正确！",Null)
		Return
	End If
	'' 通过后生生jwt
	Dim jwt As String=JwtHelper.CreateJWT(dbUserId) ''授权token 6小时失效
	Dim rjwt As String=JwtHelper.CreateRefreshToken(dbUserId) ''刷新token 7天失效
	'' 组装返回结果
	Dim resultmp As Map
	resultmp.Initialize
	resultmp.Put("token",jwt)
	resultmp.Put("refreshToken",rjwt)
	DateTime.DateFormat="yyyy-MM-dd HH:mm:ss"
	resultmp.Put("createDate",DateTime.Date(DateTime.Now))
	'' 返回token给前端
	'' resp.Write(resultmp.As(JSON).ToString)
	AjaxResult.Success(resp,"登录成功！",resultmp.As(JSON))
End Sub

'' 用户注册
Private Sub AccountRegister(req As ServletRequest, resp As ServletResponse)
	'' resp.Write("用户注册返回结果")
	Dim result As Map= redis.HGetAll("vsto_office:sys_config:sys.index.sideTheme")
	AjaxResult.Success(resp,"用户注册返回结果！",result)
End Sub

'' 忘记密码
Private Sub AccountForgotPassword(req As ServletRequest, resp As ServletResponse)
	'' resp.Write("忘记密码返回结果")
	AjaxResult.Success(resp,"忘记密码返回结果！",Null)
End Sub

'' 获取用户西信息 （需要登录认证）
Private Sub GetUserInfo(req As ServletRequest, resp As ServletResponse)
	'' 取出token中的用户id，确保仅获取当前登录用户的信息
	Dim token As String=req.GetHeader("token")
	'' 解析token
	Dim userId As String=JwtHelper.GetUsernameFromJWT(token)
	Dim result As List= mysql.ExecuteQueryByList("select * from sys_user where user_id=?",Array As String(userId))
	'' resp.Write(result.As(JSON).ToString)
	AjaxResult.Success(resp,"获取成功！",result.As(JSON))
End Sub